package com.bstek.pms.tools.service.impl;

import com.bstek.pms.tools.model.DBProperties;
import com.bstek.pms.tools.model.TableInfo;
import com.bstek.pms.tools.service.DBInfoService;
import com.bstek.pms.tools.service.ExportDocument;
import com.bstek.pms.tools.utils.FileUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @Author zhangkun
 * @CreateDate 2020/8/1
 */
public class ExportDocumentForWord implements ExportDocument {

    private Map<String, Object> params;
    private DBProperties properties;

    public ExportDocumentForWord(Map<String, Object> params, DBProperties properties) {
        this.params = params;
        this.properties = properties;
    }

    @Override
    public void export() throws Exception {
        //生成文件输出目录
        String exportFilePath = FileUtils.generateExportDirs(params.get("sysName") + "_" + "数据结构变更");

        //计算选中的数据行,从数据库查询表结构，写入到excel中
        List<TableInfo> selectedTabs = DBInfoService.getSelectedTabs(properties, params);

        XWPFDocument document = new XWPFDocument();

        //文档输出
        FileOutputStream out = new FileOutputStream(exportFilePath + File.separator + params.get("sysName") + "_" + "数据结构变更文档.docx");
        try{
            document.write(out);
        }finally {
            out.close();
        }
    }
}
